#ifndef CIRCLEPROGRESSBAR_H
#define CIRCLEPROGRESSBAR_H

#include <QPainter>
#include <QTimer>
#include <QWidget>
#include <QGraphicsView>
#include <QPaintEvent>
#include <QPainterPath>
#include <QtMath>
#include <QStaticText>
#include <QDateTime>
#include "../../protocol/visibilityprotocol.h"


#define CIRCLE_PROGRESS_ONE_MINUTE_VISIBILITY_MAX   80000

class CircleProgressBar : public QWidget
{
    Q_OBJECT
public:

    explicit CircleProgressBar(QWidget* widget = nullptr);

public slots:
    void Slot_UpdateValue(protocol::VisibilityProtocol protocol){
        progress = protocol.m_one_minute_visibility;
        this->update();
    };

protected:

    void paintEvent(QPaintEvent *event) override
    {
        Q_UNUSED(event);

        QPainter painter(this);
        painter.fillRect(0, 0, width(), height(), Qt::white);

        // 中心坐标和半径
        int centerX = width() / 2;
        int centerY = height() / 2;
        float radiu = qMin(width(), height())/2.5;
        float diff_x = centerX - radiu;
        float diff_y = centerY - radiu;

        /*
         * 画笔
        */
        painter.setPen(QPen(QColor("#0ea3ff"), 4, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin));

        // 每次绘制线条角度的步长
        float increment = 5;
        /*
         * 绘制线条
        */
        for (int i = 0; i <= 54; i++) {
            float angle = (i * increment - 90) / 180 * M_PI;
            QPointF point_start;
            point_start.rx() = (radiu - 20) * qCos(angle);
            point_start.ry() = (radiu - 20) * qSin(angle);
            if(point_start.rx() < centerX ){
                point_start.rx() = centerX - point_start.rx();
            }
            if(point_start.ry() < centerY){
                point_start.ry() = centerY - point_start.ry();
            }

            QPointF point_end;
            point_end.rx() = (radiu + 20) * qCos(angle);
            point_end.ry() = (radiu + 20) * qSin(angle);
            if(point_end.rx() < centerX ){
                point_end.rx() = centerX - point_end.rx();
            }
            if(point_end.ry() < centerY){
                point_end.ry() = centerY - point_end.ry();
            }

            painter.drawLine(point_start,point_end);
        }

        /*
         * 绘制图标
        */
        QRectF target(centerX - 64, centerY - 64, 128, 128);
        QRectF source(0.0, 0.0, 64, 64);
        QPixmap pixmap("://resource/visibility.png");

        painter.drawPixmap(target, pixmap, source);


        /*
         * 线条深浅表示强度
        */
        QConicalGradient gradient;
        gradient.setCenter(centerX, centerY);
        gradient.setAngle(0);
        gradient.setColorAt(0.90, QColor(Qt::transparent));
        gradient.setColorAt(0.80, QColor(199, 233, 255, 50));
        gradient.setColorAt(0.60, QColor(155, 212, 255, 100));
        gradient.setColorAt(0.50, QColor(98,  192, 255, 150));
        gradient.setColorAt(0.40, QColor(47,  141, 255, 200));
        gradient.setColorAt(0.20, QColor(8,   86,  255, 250));
        QPen pen(gradient, 45, Qt::SolidLine, Qt::SquareCap, Qt::BevelJoin);
        painter.setPen(pen);

        /*
         * 计算数据比例,默认最大50,000
        */
        double step = (double)progress / CIRCLE_PROGRESS_ONE_MINUTE_VISIBILITY_MAX * 360;

        /*
         * 绘制进度
        */
        painter.drawArc(diff_x, diff_y, radiu * 2, radiu * 2, 270 * 16, (0-step) * 16);

        /*
         * 绘制文字
        */
        painter.setPen(QPen(QColor("#0856ff"), 2, Qt::SolidLine, Qt::SquareCap, Qt::BevelJoin));
        painter.setFont(QFont("微软雅黑", 10, QFont::Bold));
        painter.drawText(centerX + radiu/4, centerY + radiu/1.5 - 40, QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss"));
        painter.setFont(QFont("微软雅黑", 26, QFont::Bold));
        QLocale locale(QLocale::English);
        QString num_progress = locale.toString(progress);
        painter.drawText(centerX + radiu/4, centerY + radiu/1.5, num_progress);
        painter.setFont(QFont("微软雅黑", 10, QFont::Bold));
        painter.drawText(centerX + radiu/4, centerY + radiu/1.5 + 40, "一分钟能见度");
    }

private:
    double progress;

};

#endif // CIRCLEPROGRESSBAR_H
